import { RequestRs } from '../../modules/Request-rs';
import { ReponseRs } from '../../modules/Response-rs';
import { Component, OnInit } from '@angular/core';
import {Product} from '../../modules/product';
import { User } from '../../modules/user';
import { HttpAssist } from '../../share/assist/http-assist';
import {ActivatedRoute} from '@angular/router';
import {HttpService} from '../../share/service/http.service';
import { UserService } from '../../share/service/user.service';
import { ShoppingCartComponent } from '../shopping-cart/shopping-cart.component';
import {HttpParams} from '@angular/common/http';


@Component({
  selector: 'app-product-details',
  templateUrl: './product-details.component.html',
  styleUrls: ['./product-details.component.css']
})
export class ProductDetailsComponent implements OnInit {
  curUser: User;
  req: RequestRs = new RequestRs();
  res: ReponseRs = new ReponseRs();
  product: Product;
  comments: Comment[];
  serviceName: 'getProduct';
  private productId: number;
  constructor(
    private routeInfo: ActivatedRoute,
    private httpService: HttpService,
    private curUserService: UserService
    ) {
    this.curUser = this.curUserService.getUser;
   }
  getProductDetail() {
    const serviceName = 'getProduct';
    this.req.path = '/' + this.productId;
    this.req.param = '';
    this.req.body = '';
    const message = JSON.stringify(this.req);
    this.httpService.postByJson(serviceName,HttpAssist.GET_PATH,message).subscribe(
      s => {
        this.res = s as ReponseRs;
        console.info(serviceName + ' ' + this.res.status);
        console.info(this.res.message);
        if (this.res.status === HttpAssist.RS_SUCCESS) {
          this.product =  JSON.parse(this.res.message);
        }else {
          alert('fail, detail{' + 'code:' + this.res.status + 'message:'+ this.res.message+'}');
        }
      },
      error => {
          alert('fail,http error in UI, detail' + error);
      }
    );
  }
  public addProductToSC() {
    const serviceName = 'addProductToSC';
    this.req.path = '/' + this.curUser.id + '/' + this.product.id;
    this.req.param = '';
    this.req.body = '';
    const message = JSON.stringify(this.req);
    this.httpService.postByJson(serviceName, HttpAssist.GET_PATH, message).subscribe(
      s => {
        this.res = s as ReponseRs;
        console.info(serviceName + ' ' + this.res.status);
        console.info(this.res.message);
        if (this.res.status === HttpAssist.RS_SUCCESS) {
          alert('The product have been added to the shopping cart!');
        } else {
          alert('fail, detail{' + 'code:' + this.res.status + 'message:' + this.res.message + '}');
        }
      },
      error => {
        alert('fail,http error in UI, detail' + error);
      }
    );
  }
  ngOnInit() {
    this.product = new Product();
    this.product.desc = '';
    this.product.price = '';
    this.product.title = '';
    this.productId = this.routeInfo.snapshot.params['productId'];
    this.getProductDetail();
  }



}
